package org.brownflat.lighthouse.ci.hudson;

import org.brownflat.lighthouse.ci.ConnectionException;
import org.brownflat.lighthouse.ci.HTTPServer;
import org.brownflat.lighthouse.ci.Monitorable;

public class HudsonServer extends HTTPServer implements Monitorable {

    private String[] views = {""};

	public HudsonServer(String host, int port) {
		super(host, port);		
	}

    public void setViews(String[] views) {
        this.views = views;
    }

	@Override
	public boolean hasAnyBrokenBuilds() {
        boolean result = false;

        for(String view : views) {
            HudsonLatestBuildsRequest request = new HudsonLatestBuildsRequest(view);
            String response = null;
            try {
                response = execute(request);
            } catch (ConnectionException e) {
                throw new RuntimeException(e);
            }
            if(response.contains("FAILURE") || response.contains("UNSTABLE")){
                return true;
            }
        }
        return false;
    }
}
